package com.stylefeng.guns.rest.modular.user;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.api.user.UserApi;
import com.stylefeng.guns.api.user.UserInfoModule;
import com.stylefeng.guns.api.user.UserModule;
import com.stylefeng.guns.core.util.MD5Util;
import com.stylefeng.guns.rest.common.persistence.dao.UserMapper;
import com.stylefeng.guns.rest.common.persistence.model.MoocUserT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author sun7ay
 * Created on  2019-01-20
 */
@Service(interfaceClass = UserApi.class)
@Component
public class UserApiServiceImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;


    @Override
    public int login(String userName, String password) {
        MoocUserT moocUserT = new MoocUserT();
        moocUserT.setUserName(userName);
        MoocUserT one = userMapper.selectOne(moocUserT);
        if(one!=null){
            if(one.getUserPwd().equals(MD5Util.encrypt(password))){
                return one.getUuid();
            }
        }
        return 0;
    }

    @Override
    public boolean register(UserModule userModule) {
        MoocUserT moocUserT = new MoocUserT();
        moocUserT.setAddress(userModule.getAddress());
        moocUserT.setUserName(userModule.getUserName());
        moocUserT.setEmail(userModule.getEmail());
        moocUserT.setUserPhone(userModule.getPhone());
        moocUserT.setBirthday(userModule.getBirthday());
        moocUserT.setUserPwd(MD5Util.encrypt(userModule.getPassword()));
        return userMapper.insert(moocUserT)>0;
    }

    @Override
    public boolean checkUserName(String userName) {
        EntityWrapper<MoocUserT> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("user_name",userName);
        Integer count = userMapper.selectCount(entityWrapper);
        if(count!=null&&count>0){
            return false;
        }
        return true;
    }

    @Override
    public UserInfoModule getUserInfo(int uuid) {
        return null;
    }

    @Override
    public UserInfoModule updateUserInfo(UserInfoModule module) {
        return null;
    }
}
